﻿/// <reference path="/umbraco_client/Application/NamespaceManager.js" />

Umbraco.Sys.registerNamespace("TheOutfield.UmbExt.UniversalMediaPicker.Controls");

(function ($) {
    TheOutfield.UmbExt.UniversalMediaPicker.Controls.UniversalMediaPicker = function (label, mediaIdValueClientID, mediaTitleClientID, mediaPickerUrl, width, height, umbracoPath) 
    {
        return {
            _mediaPickerUrl: mediaPickerUrl,
            _label: label,
            _width: width,
            _height: height,
            _mediaIdValueClientID: mediaIdValueClientID,
            _mediaTitleClientID: mediaTitleClientID,
            LaunchPicker: function () 
            {
                var _this = this;
                UmbClientMgr.openModalWindow(this._mediaPickerUrl, this._label, true, this._width, this._height, 30, 0, ['#cancelbutton'], function (e) { _this.SaveSelection(e); });
            },
            SaveSelection: function (e) 
            {
                if (!e.outVal) return;
                var obj = JSON.parse(e.outVal);
                $("#" + this._mediaIdValueClientID).val(obj.Id);
                $("#" + this._mediaTitleClientID).html(obj.Title);
                $("#" + this._mediaTitleClientID).parent().show();
            },
            ClearSelection: function () 
            {
                $("#" + this._mediaTitleClientID).parent().hide();
                $("#" + this._mediaIdValueClientID).val('');
            }
        };
    }
})(jQuery);